<?php

declare(strict_types=1);

namespace app\model;

// 引入框架内置类
use think\Model;
use think\facade\Cache;

/**
 * +----------------------------------------------------------------------
 * | 系统设置模型
 * +----------------------------------------------------------------------
 */
class System extends Model
{
    protected $schema = [
        "id" => "int",  // 编号
        "name" => "varchar",  // 网站名称
        "logo" => "varchar",  // 网站LOGO
        "icp" => "varchar",  // 备案号
        "copyright" => "varchar",  // 版权信息
        "url" => "varchar",  // 网站地址
        "address" => "varchar",  // 公司地址
        "contacts" => "varchar",  // 联系人
        "tel" => "varchar",  // 联系电话
        "mobile_phone" => "varchar",  // 手机号码
        "fax" => "varchar",  // 传真号码
        "email" => "varchar",  // 邮箱账号
        "qq" => "varchar",  // QQ
        "qrcode" => "varchar",  // 公众号二维码
        "qrcode_desc" => "varchar",  // 公众号二维码
        "title" => "varchar",  // SEO标题
        "key" => "varchar",  // SEO关键字
        "des" => "varchar",  // SEO描述
        "other" => "varchar",  // 其他
        "upload_driver" => "tinyint",  // 上传驱动
        "upload_file_size" => "varchar",  // 文件限制
        "upload_file_ext" => "varchar",  // 文件格式
        "upload_image_size" => "varchar",  // 图片限制
        "upload_image_ext" => "varchar",  // 图片格式
        "editor" => "tinyint",  // 编辑器
        "message_send_mail" => "tinyint", // 留言邮件通知
        "email_host" => "varchar",  // smtp 服务器
        "email_username" => "varchar",  // 邮箱账号
        "email_password" => "varchar",  // 邮箱密码
        "email_secure" => "varchar",  // 安全协议
        "email_port" => "int",  // 服务器端口
        "email_from" => "varchar",  // 发件人
        "email_to" => "varchar",  // 邮件通知对象
        "tongji" => "varchar",  // 统计代码
    ];

    protected $autoWriteTimestamp = false; 

    public static function getSystem()
    {
        $system = Cache::get("system");

        if ($system) {
            return $system;
        }

        $data = self::field('"name","logo","icp","copyright","url","address","contacts","tel","mobile_phone","fax",
            "email","qq","qrcode","qrcode_desc","title","key","des","other"')->find(1);
        
        Cache::set('system', $data);
    
        return $data;
    }

    public static function saveData($param = [])
    {
        Cache::delete('system');
        
        self::cache(true)->where('id', 1)->update($param);
    }
}
